﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace lchealthy
{
    public partial class frmaddbusiness : Form
    {
        public int id = 0;
        public int oldnum = 0;
        public string oldtype = "借出";
        public int oldpid = 0;
        public frmaddbusiness()
        {
            InitializeComponent();
        }
        public frmaddbusiness(int mid)
        {
            id = mid;
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string type = "借出";
            if (!rdbout.Checked)
                type = "借入";
            string name = cmbCname.Text;
            int pid = int.Parse(cmbPname.SelectedValue.ToString());
            DateTime date = dateTimePicker1.Value;
            int num = int.Parse(txtNum.Text);
            string sql = "";
            if (id == 0)
            {
                sql = string.Format("insert into businessinfo(borrowtype,bname,pid,pnum,did,bdate) values('{0}','{1}',{2},{3},{4},#{5}#)", type, name, pid, num, userinfo.id, date.ToShortDateString());
                if (DbHelperOleDb.ExecuteSql(sql) == 1)
                {
                    MessageBox.Show("增加成功!");
                    if (type == "借出")
                    {
                        sql = "update inventory set inum=inum-" + num + " where did=" + userinfo.id + " and pid=" + pid;
                        DbHelperOleDb.ExecuteSql(sql);

                    }
                    else
                    {
                        sql = "SELECT pid FROM inventory where Pid=" + pid + " and did=" + userinfo.id;
                        DataSet da = DbHelperOleDb.Query(sql); 
                        if (da.Tables[0].Rows.Count == 1)
                        {                            
                            sql = string.Format("update inventory set [inum] = [inum]+{0} where [Pid] = {1} and did={2}", num, pid,userinfo.id);
                            DbHelperOleDb.ExecuteSql(sql);
                        }
                        else
                        {
                            sql = string.Format("INSERT INTO inventory ([Pid] ,[inum],[did] ) VALUES ({0},{1},{2})", pid, num, userinfo.id);
                            DbHelperOleDb.ExecuteSql(sql);
                        }              

                    }
                    this.Close();
                }
                else
                {
                    MessageBox.Show("增加失败!");
                }

            }
            else
            {
                sql = string.Format("update businessinfo set borrowtype='{0}',bname='{1}',pid={2},pnum={3},bdate=#{4}# where id={5}", type, name, pid, num, date.ToShortDateString(), id);
              
                if (DbHelperOleDb.ExecuteSql(sql) == 1)
                {
                    MessageBox.Show("修改成功!");
                    if (oldtype == type)
                    {
                        if (oldpid == pid)
                        {
                            if (num != oldnum)
                            {
                                int numsub = num - oldnum;
                                if (type == "借出")
                                {
                                    sql = "update inventory set inum=inum-" + numsub + " where did=" + userinfo.id + " and pid=" + pid;
                                    DbHelperOleDb.ExecuteSql(sql);

                                }
                                else
                                {
                                    sql = "SELECT pid FROM inventory where Pid=" + pid + " and did=" + userinfo.id;
                                    DataSet da = DbHelperOleDb.Query(sql);
                                    if (da.Tables[0].Rows.Count == 1)
                                    {
                                        sql = string.Format("update inventory set [inum] = [inum]+{0} where [Pid] = {1} and did={2}", numsub, pid, userinfo.id);
                                        DbHelperOleDb.ExecuteSql(sql);
                                    }
                                    else
                                    {
                                        sql = string.Format("INSERT INTO inventory ([Pid] ,[inum],[did] ) VALUES ({0},{1},{2})", pid, numsub, userinfo.id);
                                        DbHelperOleDb.ExecuteSql(sql);
                                    }

                                }
                            }
                        }
                        else
                        {

                            //int numsub = num - oldnum;
                            if (type == "借出")
                            {
                                sql = "update inventory set inum=inum+" + oldnum + " where did=" + userinfo.id + " and pid=" + oldpid;
                                DbHelperOleDb.ExecuteSql(sql);
                                sql = "update inventory set inum=inum-" + num + " where did=" + userinfo.id + " and pid=" + pid;
                                DbHelperOleDb.ExecuteSql(sql);

                            }
                            else
                            {
                                //修改错的pid
                                sql = string.Format("update inventory set [inum] = [inum]-{0} where [Pid] = {1} and did={2}", oldnum, oldpid, userinfo.id);
                                DbHelperOleDb.ExecuteSql(sql);

                                sql = "SELECT pid FROM inventory where Pid=" + pid + " and did=" + userinfo.id;
                                DataSet da = DbHelperOleDb.Query(sql);
                                if (da.Tables[0].Rows.Count == 1)
                                {
                                    sql = string.Format("update inventory set [inum] = [inum]+{0} where [Pid] = {1} and did={2}", num, pid, userinfo.id);
                                    DbHelperOleDb.ExecuteSql(sql);
                                }
                                else
                                {
                                    sql = string.Format("INSERT INTO inventory ([Pid] ,[inum],[did] ) VALUES ({0},{1},{2})", pid, num, userinfo.id);
                                    DbHelperOleDb.ExecuteSql(sql);
                                }
                            }

                        }
                    }
                    else
                    {
                        //类型也被修改

                        if (oldpid == pid)
                        {
                            if (num != oldnum)
                            {
                                int numsub = num - oldnum;
                                if (type == "借出")
                                {
                                    sql = "update inventory set inum=inum-" + num + oldnum + " where did=" + userinfo.id + " and pid=" + pid;
                                    DbHelperOleDb.ExecuteSql(sql);

                                }
                                else
                                {
                                    sql = "SELECT pid FROM inventory where Pid=" + pid + " and did=" + userinfo.id;
                                    DataSet da = DbHelperOleDb.Query(sql);
                                    if (da.Tables[0].Rows.Count == 1)
                                    {
                                        sql = string.Format("update inventory set [inum] = [inum]+{0} where [Pid] = {1} and did={2}", num + oldnum, pid, userinfo.id);
                                        DbHelperOleDb.ExecuteSql(sql);
                                    }
                                    else
                                    {
                                        sql = string.Format("INSERT INTO inventory ([Pid] ,[inum],[did] ) VALUES ({0},{1},{2})", pid, num + oldnum, userinfo.id);
                                        DbHelperOleDb.ExecuteSql(sql);
                                    }

                                }
                            }
                            else
                            {
                                if (type == "借出")
                                {
                                    sql = "update inventory set inum=inum-" + 2 * num + " where did=" + userinfo.id + " and pid=" + pid;
                                    DbHelperOleDb.ExecuteSql(sql);
                                }
                                else
                                {
                                    sql = "SELECT pid FROM inventory where Pid=" + pid + " and did=" + userinfo.id;
                                    DataSet da = DbHelperOleDb.Query(sql);
                                    if (da.Tables[0].Rows.Count == 1)
                                    {
                                        sql = string.Format("update inventory set [inum] = [inum]+{0} where [Pid] = {1} and did={2}", 2 * num, pid, userinfo.id);
                                        DbHelperOleDb.ExecuteSql(sql);
                                    }
                                    else
                                    {
                                        sql = string.Format("INSERT INTO inventory ([Pid] ,[inum],[did] ) VALUES ({0},{1},{2})", pid, 2 * num, userinfo.id);
                                        DbHelperOleDb.ExecuteSql(sql);
                                    }

                                }


                            }
                        }
                        else
                        {
                            if (type == "借出")
                            {
                                sql = "update inventory set inum=inum-" + oldnum + " where did=" + userinfo.id + " and pid=" + oldpid;
                                DbHelperOleDb.ExecuteSql(sql);
                                sql = "update inventory set inum=inum-" + num + " where did=" + userinfo.id + " and pid=" + pid;
                                DbHelperOleDb.ExecuteSql(sql);

                            }
                            else
                            {
                                //借入
                                //修改错的pid
                                sql = "SELECT pid FROM inventory where Pid=" + oldpid + " and did=" + userinfo.id;
                                DataSet da = DbHelperOleDb.Query(sql);
                                if (da.Tables[0].Rows.Count == 1)
                                {
                                    sql = string.Format("update inventory set [inum] = [inum]+{0} where [Pid] = {1} and did={2}", oldnum, oldpid, userinfo.id);
                                    DbHelperOleDb.ExecuteSql(sql);
                                }
                                else
                                {
                                    sql = string.Format("INSERT INTO inventory ([Pid] ,[inum],[did] ) VALUES ({0},{1},{2})", oldpid, oldnum, userinfo.id);
                                    DbHelperOleDb.ExecuteSql(sql);
                                }

                                sql = "SELECT pid FROM inventory where Pid=" + pid + " and did=" + userinfo.id;
                                da = DbHelperOleDb.Query(sql);
                                if (da.Tables[0].Rows.Count == 1)
                                {
                                    sql = string.Format("update inventory set [inum] = [inum]+{0} where [Pid] = {1} and did={2}", num, pid, userinfo.id);
                                    DbHelperOleDb.ExecuteSql(sql);
                                }
                                else
                                {
                                    sql = string.Format("INSERT INTO inventory ([Pid] ,[inum],[did] ) VALUES ({0},{1},{2})", pid, num, userinfo.id);
                                    DbHelperOleDb.ExecuteSql(sql);
                                }
                            }

                        }
                    }
                    this.Close();
                }
                else
                {
                    MessageBox.Show("修改失败!");
                }
            }


        }
        private void bind()
        {
            string sql="";
            if (!rdbout.Checked)
                sql = "SELECT PID, PName FROM ProductInfo ";
            else
            {
                sql = "SELECT ProductInfo.PID, PName FROM ProductInfo LEFT JOIN inventory ON ProductInfo.PID = inventory.Pid where inventory.did= " + userinfo.id;
            }            
            DataSet ds = DbHelperOleDb.Query(sql);
            cmbPname.DataSource = ds.Tables[0];
            cmbPname.DisplayMember = "Pname";
            cmbPname.ValueMember = "PID";
            sql = "select did,dname from dietitianinfo";
            ds = DbHelperOleDb.Query(sql);
            cmbCname.DataSource = ds.Tables[0];
            cmbCname.DisplayMember = "dname";
            cmbCname.ValueMember = "did";
        }

        private void frmaddbusiness_Load(object sender, EventArgs e)
        {
            bind();
            if (id != 0)
            {
                string sql = "select borrowtype,bname,pid,pnum,did,bdate from businessinfo where id=" + id;
                DataSet ds = DbHelperOleDb.Query(sql);
                if (ds.Tables[0].Rows.Count == 1)
                {
                    if (ds.Tables[0].Rows[0]["borrowtype"].ToString() == "借入")
                    {
                        radioButton2.Checked = true;
                        oldtype = "借入";
                    }
                    cmbCname.Text = ds.Tables[0].Rows[0]["bname"].ToString();
                    cmbPname.SelectedValue = ds.Tables[0].Rows[0]["pid"].ToString();
                    oldpid = int.Parse(ds.Tables[0].Rows[0]["pid"].ToString());
                    txtNum.Text = ds.Tables[0].Rows[0]["pnum"].ToString();
                    oldnum = int.Parse(txtNum.Text);
                    dateTimePicker1.Value = DateTime.Parse(ds.Tables[0].Rows[0]["bdate"].ToString());
                }
            }
        }

        private void rdbout_CheckedChanged(object sender, EventArgs e)
        {
            string sql = "";
            if (!rdbout.Checked)
                sql = "SELECT PID, PName FROM ProductInfo ";
            else
            {
                sql = "SELECT ProductInfo.PID, PName FROM ProductInfo LEFT JOIN inventory ON ProductInfo.PID = inventory.Pid where inventory.did= " + userinfo.id;
            }
            DataSet ds = DbHelperOleDb.Query(sql);
            cmbPname.DataSource = ds.Tables[0];
            cmbPname.DisplayMember = "Pname";
            cmbPname.ValueMember = "PID";

        }
    }
}
